package l1j.server.server.datatables;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Map;

import l1j.server.L1DatabaseFactory;
import l1j.server.server.model.Instance.L1ItemInstance;
import l1j.server.server.utils.SQLUtil;
import l1j.server.server.utils.collections.Maps;

public class BrandRuneTable {
	private static BrandRuneTable _instance = new BrandRuneTable();

	public static BrandRuneTable getInstance() {
		return _instance;
	}
	
	public static void initilize() {
		_instance = new BrandRuneTable();
	}

	private Map<String, BrandRuneSetDetail> _allBrandRunes = Maps.newMap();

	private BrandRuneTable() {
		Connection con = null;
		PreparedStatement pstm = null;
		ResultSet rs = null;
		try {
			con = L1DatabaseFactory.getInstance().getConnection();
			pstm = con.prepareStatement("SELECT * FROM x_brand_rune_set");

			rs = pstm.executeQuery();
			while (rs.next()) {

				BrandRuneSetDetail r = new BrandRuneSetDetail();
				r.setId(rs.getInt("id"));
				r.setRune_set_name(rs.getString("rune_set_name"));
				r.setRune_set(rs.getString("rune_set"));
				r.setRune_type(rs.getString("rune_type"));
				r.setRune_1(rs.getString("rune_1"));
				r.setRune_2(rs.getString("rune_2"));
				r.setRune_3(rs.getString("rune_3"));

				_allBrandRunes.put(rs.getString("rune_set"), r);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			SQLUtil.close(rs);
			SQLUtil.close(pstm);
			SQLUtil.close(con);
		}
	}
	
	public String getBrandRuneSetName(L1ItemInstance weapon) {
		int rune_1 = weapon.getArmorRuneOne();
		int rune_2 = weapon.getArmorRuneTwo();
		int rune_3 = weapon.getArmorRuneThree();
		String runeSetName = getRuneEnName(rune_1) 
							+ getRuneEnName(rune_2)
							+ getRuneEnName(rune_3);
		String pcRuneSet = String.valueOf(rune_1) 
							+ String.valueOf(rune_2) 
							+ String.valueOf(rune_3);
		for (int i = 0; i < _allBrandRunes.size(); i++) {
			if (_allBrandRunes.containsKey(pcRuneSet)) {
				runeSetName = _allBrandRunes.get(pcRuneSet).getRune_set_name();
				weapon.setBrandRuneSetId(_allBrandRunes.get(pcRuneSet).getId());
				break;
			}
		}
		return runeSetName;
	}
	
	private String getRuneEnName(int rune) {
		String runeEnName = "";
		switch(rune) {
		case 1:
			runeEnName = "A";
			break;
		case 2:
			runeEnName = "B";
			break;
		case 3:
			runeEnName = "C";
			break;
		case 4:
			runeEnName = "D";
			break;
		case 5:
			runeEnName = "E";
			break;
		case 6:
			runeEnName = "F";
			break;
		default:
			;
			break;	
		}
		return runeEnName;
	}

	public class BrandRuneSetDetail {
		private int id = 0;
		private String rune_set_name = "";
		private String rune_type = "";
		private String rune_set = "";
		private String rune_1 = "";
		private String rune_2 = "";
		private String rune_3 = "";
		
		public int getId() {
			return id;
		}
		public void setId(int id) {
			this.id = id;
		}
		public String getRune_set_name() {
			return rune_set_name;
		}
		public void setRune_set_name(String rune_set_name) {
			this.rune_set_name = rune_set_name;
		}
		public String getRune_type() {
			return rune_type;
		}
		public void setRune_type(String rune_type) {
			this.rune_type = rune_type;
		}
		public String getRune_set() {
			return rune_set;
		}
		public void setRune_set(String rune_set) {
			this.rune_set = rune_set;
		}
		public String getRune_1() {
			return rune_1;
		}
		public void setRune_1(String rune_1) {
			this.rune_1 = rune_1;
		}
		public String getRune_2() {
			return rune_2;
		}
		public void setRune_2(String rune_2) {
			this.rune_2 = rune_2;
		}
		public String getRune_3() {
			return rune_3;
		}
		public void setRune_3(String rune_3) {
			this.rune_3 = rune_3;
		}
	}
}
